<?php

namespace App\Home\Middleware;

use App\Home\Traits\UrlWhitelist;
use App\Home\Utils\Home;
use Illuminate\Http\Request;
use Illuminate\Support\Str;

class HomePermission
{
    use UrlWhitelist;

    /**
     * @var array url 白名单
     */
    protected $urlWhitelist = [
        '/auth/login',
        '/auth/logout',
        '/user',
        '/user/edit',
        '/configs/vue-routers',
        'GET:/configs/system_basic/values',
    ];

    /**
     * Handle an incoming request.
     *
     * @param \Illuminate\Http\Request $request
     * @param \Closure $next
     * @param array $args
     *
     * @return mixed
     */
    public function handle(Request $request, \Closure $next, ...$args)
    {
        if (!empty($args) || $this->shouldPassThrough($request)) {
            return $next($request);
        }
        return $next($request);
    }

    protected function urlWhitelist(): array
    {
        return array_map(function ($url) {
            return Home::urlWithMethod($url);
        }, $this->urlWhitelist);
    }
}
